<?php

namespace app\common\model\kzvote;

use addons\kzvote\library\Subscription;
use addons\kzvote\library\WechatSubscription;
use think\Model;


class Vote extends Model
{

    // 表名
    protected $name = 'kzvote_vote';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'mold_text',
        'start_time_text',
        'end_time_text',
        'day_limit_status_text',
        'verify_status_text',
        'apply_status_text',
        'end_apply_status_text',
        'apply_start_time_text',
        'apply_end_time_text',
        'gift_status_text',
        'group_status_text',
        'status_text',
        'check_text',
        'is_open_text',
        'apply_check_text',
        'fans_status_text',
    ];

    protected static function init()
    {
        $platform = request()->param('platform');
        self::afterUpdate(function ($row) use ($platform){
            if ($row->origin['check'] != $row->data['check'] && $row['user_id'] > 0){
                //审核结果有变动则进入到这里，发送订阅消息通知活动创建人
                if ($platform == 'wxapp'){
                    Subscription::vote_check(kzvote_getOpenid($row['user_id'],$platform),'pages/vote/index?vote_id='.$row['id'],$row['name'],
                        date('Y-m-d H:i:s',$row['createtime']),$row->data['check']=='1'?'审核通过':'审核驳回');
                }elseif ($platform == 'wechat'){
                    WechatSubscription::vote_check(kzvote_getOpenid($row['user_id'],$platform),'pages/vote/index?vote_id='.$row['id'],$row['name'],
                        date('Y-m-d H:i:s',$row['createtime']),$row->data['check']=='1'?'审核通过':'审核驳回');
                }
            }
        });
    }
    
    public function getMoldList()
    {
        return ['1' => __('Mold 1'), '2' => __('Mold 2'), '4' => __('Mold 4')];
    }

    public function getDayLimitStatusList()
    {
        return ['0' => __('Day_limit_status 0'), '1' => __('Day_limit_status 1')];
    }

    public function getVerifyStatusList()
    {
        return ['0' => __('Verify_status 0'), '1' => __('Verify_status 1')];
    }

    public function getApplyStatusList()
    {
        return ['0' => __('Apply_status 0'), '1' => __('Apply_status 1')];
    }

    public function getEndApplyStatusList()
    {
        return ['0' => __('End_apply_status 0'), '1' => __('End_apply_status 1')];
    }

    public function getGiftStatusList()
    {
        return ['0' => __('Gift_status 0'), '1' => __('Gift_status 1')];
    }

    public function getGroupStatusList()
    {
        return ['0' => __('Group_status 0'), '1' => __('Group_status 1')];
    }

    public function getStatusList()
    {
        return ['10' => __('Status 10'), '20' => __('Status 20'), '30' => __('Status 30')];
    }

    public function getCheckList()
    {
        return ['0' => __('Check 0'), '1' => __('Check 1'), '2' => __('Check 2')];
    }

    public function getIsOpenList()
    {
        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
    }

    public function getApplyCheckList()
    {
        return ['0' => __('Apply_check 0'), '1' => __('Apply_check 1')];
    }

    public function getFansStatusList()
    {
        return ['0' => __('Fans_status 0'), '1' => __('Fans_status 1')];
    }


    public function getMoldTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['mold']) ? $data['mold'] : '');
        $list = $this->getMoldList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getDayLimitStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['day_limit_status']) ? $data['day_limit_status'] : '');
        $list = $this->getDayLimitStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVerifyStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['verify_status']) ? $data['verify_status'] : '');
        $list = $this->getVerifyStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getApplyStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['apply_status']) ? $data['apply_status'] : '');
        $list = $this->getApplyStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getEndApplyStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_apply_status']) ? $data['end_apply_status'] : '');
        $list = $this->getEndApplyStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getApplyStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['apply_start_time']) ? $data['apply_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getApplyEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['apply_end_time']) ? $data['apply_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getGiftStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gift_status']) ? $data['gift_status'] : '');
        $list = $this->getGiftStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGroupStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['group_status']) ? $data['group_status'] : '');
        $list = $this->getGroupStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCheckTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check']) ? $data['check'] : '');
        $list = $this->getCheckList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsOpenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
        $list = $this->getIsOpenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getApplyCheckTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['apply_check']) ? $data['apply_check'] : '');
        $list = $this->getApplyCheckList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFansStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['fans_status']) ? $data['fans_status'] : '');
        $list = $this->getFansStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getFansDescAttr($value)
    {
        return json_decode($value,true);
    }

    public function getErCodeAttr($value)
    {
        if ($value){
            return request()->domain() . $value;
        }
        return null;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setApplyStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setApplyEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFansDescAttr($value,$data)
    {
        if ($data['fans_status'] == '1'){
            return json_encode($value);
        }
        return null;
    }

    public function getLinkAttr($value){
        if($value)
            return json_decode($value,true);
        else
            return null;
    }

    public function getImageAttr($value,$data){
        if (isset($data['mold']) && $data['mold'] == '4'){
            //获取项目域名
            $domain = request()->domain();
            return $domain.'/assets/addons/kzvote/img/pk.png';
        }else{
            return $value;
        }
    }

    public function music(){
        return $this->belongsTo('addons\kzvote\model\MusicModel', 'music_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function groups(){
        return $this->hasMany('app\common\model\kzvote\Group', 'vote_id', 'id')->order('code desc');
    }

    public function user(){
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
